package com.breakfast.fun.ui.user.coupon;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.adapter.user.CouponAdapter;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.data.entity.user.coupon.Coupon;
import com.breakfast.fun.event.FinishEvent;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.ui.web.WebActivity;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;
import com.breakfast.fun.view.widget.TitleBarView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by yukai on 2016/11/9.
 */

public class CouponActivity extends BaseActivity<CouponPresenter, CouponModel> implements CouponContract.View {


    @Bind(R.id.tbv_user_coupon)
    TitleBarView mTbvUserCoupon;
    @Bind(R.id.ptrlv_coupon)
    PullToRefreshListView mPtrlvCoupon;
    @Bind(R.id.iv_user_coupon_empty)
    ImageView mIvUserCouponEmpty;
    @Bind(R.id.ll_user_coupon_no_coupon)
    LinearLayout mLlUserCouponNoCoupon;
    @Bind(R.id.tv_user_coupon_info)
    TextView mTvUserCouponInfo;
    @Bind(R.id.tv_user_coupon_out)
    TextView mTvUserCouponOut;

    private CouponAdapter adapter;
    private Coupon.CouponData curCoupon;        //当前被选中的优惠券

    private int begin = 1;

    //从UserCartActivity点击选择优惠券的请求码和响应码
    private final static int COUPONREQUEST = 1;
    private final static int NOCOUPON = 2;

    private boolean isGiftActivity = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_coupon;
    }

    @Override
    public void initView() {
        isGiftActivity = getIntent().getBooleanExtra("isGiftActivity", false);
        EventBus.getDefault().register(this);
        if (!isGiftActivity) {
            mTbvUserCoupon.setAll("优惠券");
            mTbvUserCoupon.setRightImg(R.drawable.ic_problem);
            mTbvUserCoupon.setBack(this);

            mTbvUserCoupon.setRightListener(v -> {
                Intent intent = new Intent(CouponActivity.this, WebActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("title", "优惠券使用说明");
                intent.putExtra("des", "抽奖优惠券说明");
                intent.putExtra("url", "http://www.52letsgo.cn/fe/user/fqa/q_coupon.html");
                startActivity(intent);
            });

            mPtrlvCoupon.setMode(PullToRefreshBase.Mode.BOTH);
            mPtrlvCoupon.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                    begin = 0;
                    presenter.getCoupon(begin, false);
                    begin++;
                }

                @Override
                public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                    presenter.getCoupon(5 * begin, true);
                }

            });

            mLlUserCouponNoCoupon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    setResult(NOCOUPON, intent);
                    finish();
                }
            });

            mTvUserCouponOut.setOnClickListener(v -> {
                Intent intent = new Intent(CouponActivity.this, CouponUselessActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            });
        } else {
            mTbvUserCoupon.setTitleNoRight("注册大礼包");
            mTbvUserCoupon.setBack(this);
            mPtrlvCoupon.setMode(PullToRefreshBase.Mode.DISABLED);

            mTvUserCouponInfo.setOnClickListener(v -> {
                Intent intent = new Intent(CouponActivity.this, WebActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("title", "优惠券使用说明");
                intent.putExtra("des", "抽奖优惠券说明");
                intent.putExtra("url", "http://www.52letsgo.cn/fe/user/fqa/q_coupon.html");
                startActivity(intent);
            });
        }
    }

    @Override
    public void showCoupon(Coupon coupon) {
        if (!isGiftActivity) {

            Boolean isFromUser = getIntent().getBooleanExtra("isFromUser", false);
            if (isFromUser) {
                mLlUserCouponNoCoupon.setVisibility(View.GONE);
            } else {
                mLlUserCouponNoCoupon.setVisibility(View.VISIBLE);
            }
            if (coupon == null || coupon.getData() == null || coupon.getData().size() == 0) {
                mIvUserCouponEmpty.setVisibility(View.VISIBLE);
                mTvUserCouponOut.setVisibility(View.VISIBLE);
                mLlUserCouponNoCoupon.setVisibility(View.GONE);
                return;
            } else {
                mIvUserCouponEmpty.setVisibility(View.GONE);
                mLlUserCouponNoCoupon.setVisibility(View.VISIBLE);
//            begin = 10;
            }
            adapter = new CouponAdapter(context, coupon.getData());
            if ((curCoupon = (Coupon.CouponData) getIntent().getSerializableExtra("chooseCoupon")) != null) {
                adapter.setCurCoupon(curCoupon);
            } else {
                adapter.setCurCoupon(null);
            }
            adapter.setTotalPrice(getIntent().getDoubleExtra("totalPrice", 0.0));
            adapter.setIsFromUser(isFromUser);
            mPtrlvCoupon.setAdapter(adapter);
        } else {
            adapter = new CouponAdapter(context, (List<Coupon.CouponData>) getIntent().getSerializableExtra("couponList"));
            adapter.setNeedShowUseless(false);
            adapter.setIsFromUser(true);
            mPtrlvCoupon.setAdapter(adapter);
        }
    }


    @Override
    public void loadCoupon(Coupon coupon) {
        if (coupon.getData().size() == 0) {
            T.show("已无更多数据");
        } else {
            begin++;
            adapter.addALL(coupon.getData());
            adapter.notifyDataSetChanged();
        }
    }


    @Override
    public void stopRefresh() {
        mPtrlvCoupon.onRefreshComplete();
    }

//    @Subscribe
//    public void onEventPay(PayEvent event){
//        if(event.getClassName().equals(this.getClass().getSimpleName())) {
//            presenter.getPayBean(event.getOrderNo());
//        }
//    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventRefresh(RefreshEvent event) {
        if (event.getActivityName().equals(getClass().getSimpleName())) {
            begin = 0;
            presenter.getCoupon(begin, false);
            begin++;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventFinish(FinishEvent event) {
        if (event.getActivityName().equals(getClass().getSimpleName())) {
            finish();
        }
    }


//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        EventBus.getDefault().unregister(this);
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
